package com.example.xd.model_net.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @auther XuDan on 2019/6/27.
 */
public class JsonHttpResuest implements IHttpRequest {
    private String url;
    private Byte[] bytes;
    private ICallBackListener listener;

    @Override
    public void setUrl(String url) {
        this.url = url;
    }

    @Override
    public void setData(Byte[] bytes) {
        this.bytes = bytes;
    }

    @Override
    public void setListener(ICallBackListener listener) {
        this.listener = listener;
    }

    @Override
    public void execute() {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL s = new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection) s.openConnection();
            urlConnection.setConnectTimeout(8000);
            urlConnection.setRequestMethod("GET");
            urlConnection.setReadTimeout(8000);
            InputStream inputStream = urlConnection.getInputStream();
            reader = new BufferedReader((new InputStreamReader(inputStream)));
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
    }
}
